package com.pxene.dmp.test.mr.matchUser2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MatchUser2Mapper extends Mapper<LongWritable, Text, Text, Text> {
	private static List<String> words = new ArrayList<>();
	private static List<String> codes = new ArrayList<>();
	private static List<String> bbses = new ArrayList<>();
	private Text resultKey = new Text();
	private Text resultVal = new Text();

	@Override
	protected void setup(Context context) throws IOException, InterruptedException {
		words = IOUtils.readLines(MatchUser2Mapper.class.getResourceAsStream("/regex/word2.txt"));
		codes = IOUtils.readLines(MatchUser2Mapper.class.getResourceAsStream("/regex/auto.txt"));
		bbses = IOUtils.readLines(MatchUser2Mapper.class.getResourceAsStream("/regex/bbsid.txt"));
	}

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String data = value.toString();
		String kk = data.split("\t")[0];
		String vv = data.split("\t")[1];
		for (String code : codes) {
			if(code.split("---").length<2){
				continue;
			}
			String domain = code.split("---")[0];
			String autocde = code.split("---")[1];
			if (vv.startsWith(domain) && vv.endsWith(autocde)) {
				resultKey.set(kk);
				resultVal.set(vv + "  C-C");
				context.write(resultKey, resultVal);
				break;
			}
		}
		for (String word : words) {
			if (vv.contains(word)) {
				resultKey.set(kk);
				resultVal.set(vv + "  W-W");
				context.write(resultKey, resultVal);
				break;
			}
		}
		for (String bbsinfo : bbses) {
			if(bbsinfo.split("---").length<2){
				continue;
			}
			String domain=bbsinfo.split("---")[0];
			String bbsid=bbsinfo.split("---")[1];
			if (vv.endsWith(bbsid) && vv.startsWith(domain)) {
				resultKey.set(kk);
				resultVal.set(vv + "  B-B");
				context.write(resultKey, resultVal);
				break;
			}
		}
	}
}
